package com.pronosticnews.client.admin.view.main;

import com.google.gwt.core.shared.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiFactory;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.SimpleLayoutPanel;
import com.google.gwt.user.client.ui.Widget;
import com.pronosticnews.client.admin.ClientFactory;
import com.pronosticnews.client.admin.resources.PronosticnewsAdminResources;
import com.pronosticnews.client.event.InformationMessageEvent;
import com.pronosticnews.client.event.InformationMessageEventHandler;
import com.pronosticnews.client.services.UserService;
import com.pronosticnews.client.services.UserServiceAsync;
import com.pronosticnews.shared.model.UserDto;

public class MainScreenImpl extends Composite implements MainScreen {

	interface ApplayoutUiBinder extends UiBinder<Widget, MainScreenImpl> {
	}

	private static ApplayoutUiBinder binder = GWT
			.create(ApplayoutUiBinder.class);

	@UiField
	SimpleLayoutPanel listPanel;

	@UiField
	SimpleLayoutPanel editPanel;

	@UiField
	DialogBox box;

	@UiField
	Label lblMessage;

	@UiField
	Label lblLogin;
	
	@UiField(provided = true)
	PronosticnewsAdminResources res;

	private ClientFactory clientFactory;

	public MainScreenImpl(ClientFactory clientFactory) {
				
		//connexion
		UserServiceAsync userService = GWT.create(UserService.class);
		userService.getConnected(new AsyncCallback<UserDto>() {

			@Override
			public void onFailure(Throwable caught) {
				
			}

			@Override
			public void onSuccess(UserDto result) {
				lblLogin.setText(result.getLogin());
			}
		});
		
		
		this.clientFactory = clientFactory;

		res = GWT.create(PronosticnewsAdminResources.class);
		res.cssInformationBox().ensureInjected();
		res.cssMain().ensureInjected();

		initWidget(binder.createAndBindUi(this));

		clientFactory.getEventBus().addHandler(InformationMessageEvent.TYPE,
				new InformationMessageEventHandler() {

					@Override
					public void onReceive(InformationMessageEvent event) {
						lblMessage.setText(event.getMessage().getText().get(0));
						box.center();
					}
				});

	}

	@UiField
	PushButton localeUk;

	@UiField
	PushButton localeEs;

	@UiHandler({ "localeFr", "localeUk", "localeEs" })
	void onChangeLocale(ClickEvent event) {
		String locale = "fr";
		if (localeUk.equals(event.getSource())) {
			locale = "en";
		}
		if (localeEs.equals(event.getSource())) {
			locale = "es";
		}
		changeLocale(locale);
	}

	private native void changeLocale(String newLocale)/*-{
		var currLocation = $wnd.location.toString();
		var noHistoryCurrLocArray = currLocation.split("#");
		var noHistoryCurrLoc = noHistoryCurrLocArray[0];
		var locArray = noHistoryCurrLoc.split("?");
		$wnd.location.href = locArray[0] + "?locale=" + newLocale;
	}-*/;

	@UiHandler("lnkDeconnect")
	void onClickDeconnect(ClickEvent event) {
		deconnect();
	}
		
	private native void deconnect()/*-{
		$wnd.location.href = "/j_spring_security_logout";
	}-*/;

	@UiHandler("closeInfosBox")
	void onClick(ClickEvent event) {
		box.hide();
	}

	@UiFactory
	Menu createMenu() {
		return new Menu(clientFactory);
	}

	public Composite getMain() {
		return this;
	}

	@Override
	public SimpleLayoutPanel getListPanel() {
		return listPanel;
	}

	@Override
	public SimpleLayoutPanel getEditPanel() {
		return editPanel;
	}

}
